package cn.ztydata.teachingsystem.heaven.web.controller;

import cn.ztydata.teachingsystem.common.Page;
import cn.ztydata.teachingsystem.heaven.cons.MsgTemplate;
import cn.ztydata.teachingsystem.heaven.entity.Department;
import cn.ztydata.teachingsystem.heaven.entity.Student;
import cn.ztydata.teachingsystem.heaven.entity.Teacher;
import cn.ztydata.teachingsystem.heaven.entity.User;
import cn.ztydata.teachingsystem.heaven.service.DepartmentService;
import cn.ztydata.teachingsystem.heaven.service.TeacherService;
import cn.ztydata.teachingsystem.heaven.web.dto.ClassGroup.StudentInfoDto;
import cn.ztydata.teachingsystem.heaven.web.dto.SpecialtyGroup.TeacherInfoDto;
import cn.ztydata.teachingsystem.heaven.web.dto.TeacherDto;
import cn.ztydata.teachingsystem.heaven.web.dto.TeacherListDto;
import cn.ztydata.teachingsystem.heaven.web.wrapper.HttpSessionProxy;
import cn.ztydata.teachingsystem.util.StringCodeUtil;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResult;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResultData;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResultList;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResultPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.MessageFormat;
import java.util.List;

/**
 * 教师控制器类
 * Created by chenxin on 2014/11/27.
 */
@Controller
@RequestMapping("/teacher")
public class TeacherController extends BaseController {
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private DepartmentService departmentService;
    @Value("${session.key}")
    private String sessionKey;

    //<editor-fold "1--3期">
    /**
     * 教师列表
     * @param pageNum 页码
     * @param pageSize 每页显示条数
     * @param departNum 系部编码
     * @param keyword 关键字
     * @return JsonResult
     *
     * @author fanruiping
     * @since 2014-12-11
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult list(@RequestParam(value = "page",required = false, defaultValue = "1") int pageNum,
                           @RequestParam(value = "pageSize",required = false, defaultValue = "1") int pageSize,
                           @RequestParam(value = "depart",required = false, defaultValue = "") String departNum,
                           @RequestParam(value = "keyword",required = false,defaultValue = "") String keyword){
        //搜索关键字转码
        keyword = StringCodeUtil.urlParamTransCode(keyword, "utf-8");

        pageSize = pageSize < 1 ? 1 : pageSize;

        pageNum = pageNum < 1 ? 1 : pageNum;

        Page<Teacher> page = teacherService.getList(keyword,departNum,pageNum,pageSize);

        Page<TeacherDto> dtoPage = new Page<>(page.getTotal(),page.getSize(),page.getNum(),mapper.mapList(page.getItems(), TeacherDto.class));

        log.debug("教师列表");
        return new JsonResultPage<>(dtoPage);
    }

    /**
     * 重置密码
     * @param id  用户编号
     * @return JsonResult
     *
     * @author fanruiping
     * @since 2014-12-29
     */
    @RequestMapping(value = "/resetPassword/{id}", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult resetPassword(@PathVariable Long id){
        if(teacherService.changePassword("123456",id)){
            log.debug("重置密码成功:teacherService.changePassword(\"123456\",id)");
            return new JsonResult(true, "重置密码成功");
        }

        log.debug("重置密码失败");
        return new JsonResult(false,"重置密码失败");
    }

    /**
     * 获取个人详细信息(没用)
     * @param id 用户编号
     * @return JsonResultData
     *
     * @author fanruiping
     * @since 2014-11-28
     */
    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResultData detail(@PathVariable long id){
        Teacher entity = teacherService.getDetail(id);
        if(null == entity){
            log.debug("查找的教师数据不存在:null == entity");
            return new JsonResultData(false, "查找的教师数据不存在");
        }

        Department department = departmentService.getDetailByNumber(entity.getDeptNum());

        if(null != department){
            log.debug("如果系部详细为NULL：null != department");
            entity.setDeptName(department.getName());
        }

        TeacherDto dto = mapper.map(entity, TeacherDto.class);

        log.debug("获取个人详细信息");
        return new JsonResultData(dto);
    }

    /**
     * 获取首页-系部-教师列表
     *
     * @author chenxin
     * @since 2015/1/11
     */
    @RequestMapping(value = "/indexList/{deptNum}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResultList<TeacherListDto> getIndexList(@PathVariable String deptNum) {
        //校验参数
        Assert.hasText(deptNum, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "系部编码"));

        //获取首页-系部-教师信息
        List<Teacher> teachers = teacherService.getIndexList(deptNum, 5);

        log.debug("获取首页-系部-教师列表");
        return new JsonResultList<>(mapper.mapList(teachers, TeacherListDto.class));
    }

    //</editor-fold>



    //<editor-fold "专业群组">

    /**
     * 跟专业ID分页获取其教师成员
     *
     * @param specId 专业ID
     * @param letter 姓名首字母
     * @param pageNum 当前页码
     * @param pageSize 每页条数
     * @param search 关键字
     * @return JsonResultPage
     * @author lyj
     * @since 2015-5-6
     */
    @RequestMapping(value = "/majorGroupList", method = RequestMethod.GET)
    @ResponseBody
    public JsonResultPage getSpecialtyTeachers(@RequestParam("groupId") long specId,
                                               @RequestParam(value = "letter", required = false, defaultValue = "0") String letter,
                                               @RequestParam("page") int pageNum,
                                               @RequestParam("pageSize") int pageSize,
                                               @RequestParam(value = "search", required = false, defaultValue = "") String search,
                                               HttpServletRequest request) {
        Assert.isTrue(specId > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "专业ID"));
        Assert.isTrue(pageNum > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "当前页码"));
        Assert.isTrue(pageSize > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "每页条数"));

        //汉字转码
        letter = StringCodeUtil.urlParamTransCode(letter, "utf-8");
        search = StringCodeUtil.urlParamTransCode(search, "utf-8");

        //获取登录用户信息
        User userInfo = (User)((HttpSessionProxy)request.getSession()).getAttribute(sessionKey);

        Page<Teacher> page = teacherService.getSpecialtyTeachers(specId, letter, pageNum, pageSize, search, userInfo);

        log.debug("跟专业ID分页获取其教师成员");
        return new JsonResultPage(new Page(page.getTotal(), pageSize, pageNum, mapper.mapList(page.getItems(), TeacherInfoDto.class)));
    }


    /**
     * 获取活跃教师
     *
     * @param specId 专业ID
     * @param count 条数
     * @return JsonResultList
     * @author lyj
     * @since 2015-5-6
     */
    @RequestMapping(value = "/active", method = RequestMethod.GET)
    @ResponseBody
    public JsonResultList getHotTeachers(@RequestParam("groupId") long specId,
                                         @RequestParam("pageSize") int count,
                                         HttpServletRequest request) {
        Assert.isTrue(specId > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "专业ID"));
        Assert.isTrue(count > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "条数"));

        //获取登录用户信息
        User userInfo = (User)((HttpSessionProxy)request.getSession()).getAttribute(sessionKey);

        log.debug("获取活跃教师");
        return new JsonResultList(mapper.mapList(teacherService.getHotTeachers(specId, count, userInfo), TeacherInfoDto.class));
    }


    //</editor-fold>
}
